﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Xml;
using System.Xml.Linq;
using System.Text;
using System.IO;

namespace MapSample.V2
{
    public partial class Twitter : PhoneApplicationPage
    {
        private const string START_TEXT = "<div class=\"mob_ft service_update\">";
        private const string END_TEXT = "<span class=\"mob_twitter_ts\">";

        public Twitter()
        {
            InitializeComponent();
            doRequest("http://lothianbuses.com/mobile.php");
        }

        private void buttonBack_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.GoBack();
        }

        private void doRequest(String uri)
        {
            var webClient = new WebClient();
            webClient.OpenReadAsync(new Uri(uri));
            webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
        }

        void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            try
            {
                using (var reader = new StreamReader(e.Result))
                {
                    String xhtmlString = reader.ReadToEnd();
                    String xmlString = xhtmlString.Substring(xhtmlString.IndexOf(START_TEXT, START_TEXT.Length));
                    xmlString = xmlString.Substring(0, xmlString.IndexOf(END_TEXT) + END_TEXT.Length) + "</div>";
                    string news = cleanHTML(xmlString);
                    textBlock1.Text += news;
                    customIndeterminateProgressBar.Visibility = Visibility.Collapsed;
                }
            }
            catch
            {
                MessageBox.Show("Unable to connect to news feed!");
                customIndeterminateProgressBar.Visibility = Visibility.Collapsed;
                NavigationService.GoBack();
            }
        }

        public string cleanHTML(String xmlData)
        {
            int startTags = 0;
            StringBuilder builder = new StringBuilder();
            foreach (char c in xmlData.ToCharArray())
            {
                if (c == '<')
                {
                    startTags++;
                }
                else if (c == '>')
                {
                    startTags--;
                }
                else if (startTags == 0)
                {
                    builder.Append(c);
                }
            }
            String news = builder.ToString();
            news = news.Trim();
            news = news.Replace("\n", "");
            news = news.Replace("&nbsp;", " ");
            news = news.Replace("click here for details", "");
            news = news.Replace(".", ".\n");
            news = news.Replace(",", "");
            return news;
        }

    }
}